<?php

namespace app\commission;

use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

/**
 *
 * new upgrade
 *
 * Class UpGrade
 * @package app\commission
 */
class U
{
    protected $order;
    protected $set;

    public function __construct(\app\models\order\Order $order)
    {
        $this->order = $order;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 只有报单区才可以升级
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {

            // 后台设置的参数
            $this->set = SetUserUp::getLastOne();

            // 20000 升级代理
            // $total_amount = 20000;
            // $total_amount = $this->set->vip1;

            // if ($order->total_pay >= $total_amount) {
            //     $user = User::findOne(['id' => $order->user_id]);
            //     if ($user && $user->level < UserLevel::$keys['vip1']) {
            //         $user->level = UserLevel::$keys['vip1'];
            //         $user->save();
            //     }
            // }

            // myself up
            $this->upMyself();

            // chain up
            $this->chainUp();

        }
    }

    // 本人升级
    protected function upMyself()
    {
        $user = User::findOne(['id' => $this->order->user_id]);
        if (!$user) {
            throw new \Exception('user error!');
        }

        $amount = $this->order->total_pay;
        $set = $this->set;

        if ($amount >= $set->vip1) {
            if ($user->level < UserLevel::$keys['vip1']) {
                $user->level = UserLevel::$keys['vip1'];
                $user->save();
            }
        }
        if ($amount >= $set->svip1) {
            if ($user->level < UserLevel::$keys['svip1']) {
                $user->level = UserLevel::$keys['svip1'];
                $user->save();
            }
        }
        if ($amount >= $set->svip2) {
            if ($user->level < UserLevel::$keys['svip2']) {
                $user->level = UserLevel::$keys['svip2'];
                $user->save();
            }
        }
        if ($amount >= $set->svip3) {
            if ($user->level < UserLevel::$keys['svip3']) {
                $user->level = UserLevel::$keys['svip3'];
                $user->save();
            }
        }
    }

    // 关系链 升级
    protected function chainUp()
    {
        $referrer = Chain::getReferrer($this->order->user_id, true);
        if ($referrer && $referrer->level >= UserLevel::$keys['vip1']) {
            // $children = Chain::getChildrenLevel($referrer->id, UserLevel::$keys['vip1']);
            $children = Chain::getChildrenLevel($referrer->id, [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                UserLevel::$keys['svip1'],
                UserLevel::$keys['svip2'],
                UserLevel::$keys['svip3'],
            ]);
            if ($children && count($children) >= 2) {
                if ($referrer->level < UserLevel::$keys['vip2']) {
                    $referrer->level = UserLevel::$keys['vip2'];
                    $referrer->save();
                }
            }
        }

        // // 包含本人
        // $chain = Chain::getReferrerChain($user_id, [], true);
        // $user = User::findOne(['id' => $user_id]);
        // $chain = array_merge([$user], $chain);
        //
        // foreach ($chain as $u) {
        //
        //     // 必须要是 代理及以上等级
        //     if ($u->level < UserLevel::$keys['vip1']) {
        //         continue;
        //     }
        //
        //     // 直推2个代理, 成为合伙人
        //     if ($u->level < UserLevel::$keys['vip2']) {
        //         // $count = 2;
        //         $count = $this->set->vip2;
        //         $recommend = Chain::getChildren($u->id);
        //         $sum = 0;
        //         foreach ($recommend as $v) {
        //             // 需要推的是代理及以上
        //             if ($v['level'] >= UserLevel::$keys['vip1']) {
        //                 $sum += 1;
        //             }
        //         }
        //         if ($sum >= $count) {
        //             $u->level = UserLevel::$keys['vip2'];
        //             $u->save();
        //         }
        //     }
        //
        //     // 区代 | 市代
        //     if ($u->level < UserLevel::$keys['svip1'] || $u->level < UserLevel::$keys['svip2']) {
        //         // $count1 = 40;
        //         // $count2 = 100;
        //         $count1 = $this->set->svip1;
        //         $count2 = $this->set->svip2;
        //         $team = Chain::getChildrenAll($u->id, [UserLevel::$keys['vip1'],
        //             UserLevel::$keys['vip2'], UserLevel::$keys['svip1'], UserLevel::$keys['svip2']]);
        //         $sum = count($team);
        //
        //         // 团队没有包含自己
        //         if ($u->level < UserLevel::$keys['svip1'] && $sum >= $count1) {
        //             $u->level = UserLevel::$keys['svip1'];
        //             $u->save();
        //         }
        //
        //         // 团队没有包含自己
        //         if ($u->level < UserLevel::$keys['svip2'] && $sum >= $count2) {
        //             $u->level = UserLevel::$keys['svip2'];
        //             $u->save();
        //         }
        //     }
        // }
    }
}